public class Darr {
    private Object [] data = null;
    private int len = 0;
    private int travelCount = 0;

    public Darr() {}

    //添加数据到动态数组
    public Darr add(Object ... inputData) {
        int ilen = inputData.length;
        if (ilen == 0)
            return this;

        Object [] newData = new Object[this.len + ilen];
        if (this.data != null) {
            System.arraycopy(this.data, 0, newData, 0, this.len);
        }
        System.arraycopy(inputData, 0, newData, this.len, ilen);
        this.len += ilen;
        this.data = newData;

        return this;
    }

    // 动态数组元素的按下标删除
    public Darr remove(int index) {
        if (index < 0 || index >= len)
            return this;

        Object [] newData = new Object[len - 1];

        System.arraycopy(data, 0, newData, 0, index);
        System.arraycopy(data, index + 1, newData, index, len - index - 1);
        len--;
        data = newData;

        return this;
    }

    //批量删除
    //num <= 0 代表删除到结束
    public Darr remove(int start, int num) {
        if (start < 0 || start >= len)
            return this;

        num = (num <= 0) ? len : num;

        while (num-- != 0) {
            remove(start);
        }

        return this;
    }

    // 清空动态数组
    public Darr clear() {
        data = null;
        len = 0;
        return this;
    }

    //返回动态数组长度
    public int length() {
        return len;
    }

    //按下标返回动态数组的元素
    public Object ind(int index) {
        if (index < 0 || index >= len)
            return null;
        return data[index];
    }

    // 数组循环左移
    public Darr left() {
        Object t;

        for (int i = 0, l = len - 1; i < l; ++i) {
            t = data[i];
            data[i] = data[i + 1];
            data[i + 1] = t;
        }

        return this;
    }

    // 数组循环右移
    public Darr right() {
        Object t;

        for (int i = len - 1; i > 0; --i) {
            t = data[i];
            data[i] = data[i - 1];
            data[i - 1] = t;
        }

        return this;
    }

    //遍历计数器清0， 为了正常重新遍历
    public Darr rewind() {
        travelCount = 0;
        return this;
    }

    //遍历使用的条件
    public boolean next() {
        if (travelCount == len) {
            travelCount = 0;
            return false;
        }
        travelCount++;
        return true;
    }

    //返回当前遍历的数据元素
    public Object value() {
        return data[travelCount - 1];
    }
}
